package game;

//this class is for showing how many seconds are left for the player to play a card.
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Progress extends JProgressBar {
//	static Lock progressLock = new Lock();
	static int second;
	// when WuXie happens, all the heros share one countDownd
	private CountDown task;
	private Square mySquare;
	public static int PROGRESS_WIDTH;
	public static int PROGRESS_HEIGHT;
	public static void initPreferredSize() {
		PROGRESS_WIDTH = BattleField.BATTLE_FIELD_WIDTH/4;
		PROGRESS_HEIGHT = BattleField.BATTLE_FIELD_HEIGHT/32;
	}
	public Progress(int i, int j, Square linkSquare) {
		super(0, 30);
//		this.myView = null;
		setLocation(i, j);
//		setPreferredSize(new Dimension(PROGRESS_WIDTH, PROGRESS_HEIGHT));
		setSize(PROGRESS_WIDTH, PROGRESS_HEIGHT);

		second = 30;
		setValue(30);
		
		setString("Count Down");
		setBorderPainted(true);
		this.setOrientation(JProgressBar.HORIZONTAL);
//		if (task == null) {
			task = new CountDown();
			task.execute();
//		}
		task.addProgress(this);
		mySquare = linkSquare;
	}

	// associated with myView, so when the time is up, myView's cancel is
	// clicked automatically.
//	public Progress(int i, int j, View myView, Square linkSquare) {
//		super(0, 30);
////		this.myView = myView;
//		setLocation(i, j);
////		setPreferredSize(new Dimension(PROGRESS_WIDTH, PROGRESS_HEIGHT));
//
//		setSize(PROGRESS_WIDTH, PROGRESS_HEIGHT);
//		second = 30;
//		setValue(30);
//		setString("Count Down");
//		setBorderPainted(true);
//		this.setOrientation(JProgressBar.HORIZONTAL);
////		if (task == null) {
//			task = new CountDown();
//			task.execute();
////		}
//		task.addProgress(this);
//		mySquare = linkSquare;
//	}

	public void updateSecond() {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				setValue(second);
			}
		});
	}

//	View myView;
	// this happens both when the player respond, or times out
	public void finish() throws InterruptedException {
		System.out.println("Progress.finish()");
//		if (task != null) {
			task.cancel(true);
			task = null;
//		}
//		if (myView != null) {
//			myView.timesUp();
//			// @deprecated, not reasonable enough
//			// myView.cancel();
//		}
	}
	public void removeSelfFromSquare(){
			mySquare.removeProgress();
	}
}
